<template>
  <div class="title-bar">
    <div class="title-content">
      <span class="app-title">{{ currentTitle }}</span>
      <div class="document-info" v-if="currentFormulaTitle">
        - {{ currentFormulaTitle }}
      </div>
    </div>

    <div class="window-controls">
      <el-button
          link
          class="control-btn"
          @click="minimizeWindow"
          title="最小化"
      >
        <el-icon><Minus /></el-icon>
      </el-button>

      <el-button
          link
          class="control-btn"
          @click="maximizeWindow"
          title="最大化"
      >
        <el-icon><FullScreen /></el-icon>
      </el-button>

      <el-button
          link
          class="control-btn close-btn"
          @click="closeWindow"
          title="关闭"
      >
        <el-icon><Close /></el-icon>
      </el-button>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted, onUnmounted } from 'vue'
import { useFormulaStore } from '../stores/formula'
import { Minus, FullScreen, Close } from '@element-plus/icons-vue'

const formulaStore = useFormulaStore()
const currentTitle = ref('数学公式编辑器')
const currentFormulaTitle = ref('')

// 监听公式标题变化
const updateTitle = () => {
  if (formulaStore.currentFormula.title) {
    currentFormulaTitle.value = formulaStore.currentFormula.title
    if (window.electronAPI) {
      window.electronAPI.setTitle(`${formulaStore.currentFormula.title} - 数学公式编辑器`)
    } else {
      document.title = `${formulaStore.currentFormula.title} - 数学公式编辑器`
    }
  } else {
    currentFormulaTitle.value = ''
    if (window.electronAPI) {
      window.electronAPI.setTitle('数学公式编辑器')
    } else {
      document.title = '数学公式编辑器'
    }
  }
}

// 窗口控制
const minimizeWindow = () => {
  if (window.electronAPI) {
    window.electronAPI.minimizeWindow()
  }
}

const maximizeWindow = () => {
  if (window.electronAPI) {
    window.electronAPI.maximizeWindow()
  }
}

const closeWindow = () => {
  if (window.electronAPI) {
    window.electronAPI.closeWindow()
  }
}

// 监听菜单事件
onMounted(() => {
  updateTitle()

  if (window.electronAPI) {
    window.electronAPI.onMenuNewFormula(() => {
      formulaStore.createNewFormula()
    })

    window.electronAPI.onMenuSaveFormula(() => {
      formulaStore.saveFormula()
    })

    window.electronAPI.onMenuOpenOCR(() => {
      // 触发OCR功能
      const ocrButton = document.querySelector('.ocr-trigger') as HTMLElement
      ocrButton?.click()
    })

    window.electronAPI.onMenuOpenLibrary(() => {
      // 切换到公式库标签
      const libraryTab = document.querySelector('[data-tab="library"]') as HTMLElement
      libraryTab?.click()
    })
  }
})

onUnmounted(() => {
  if (window.electronAPI) {
    window.electronAPI.removeAllListeners('menu-new-formula')
    window.electronAPI.removeAllListeners('menu-save-formula')
    window.electronAPI.removeAllListeners('menu-open-ocr')
    window.electronAPI.removeAllListeners('menu-open-library')
  }
})
</script>

<style scoped>
.title-bar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0 16px;
  height: 40px;
  background: #409eff;
  color: white;
  -webkit-app-region: drag;
}

.title-content {
  display: flex;
  align-items: center;
  gap: 8px;
}

.app-title {
  font-weight: bold;
}

.document-info {
  font-size: 0.9em;
  opacity: 0.8;
}

.window-controls {
  display: flex;
  gap: 4px;
  -webkit-app-region: no-drag;
}

.control-btn {
  color: white !important;
  padding: 4px 8px !important;
  min-width: auto !important;
}

.control-btn:hover {
  background: rgba(255, 255, 255, 0.1) !important;
}

.close-btn:hover {
  background: #f56c6c !important;
}
</style>
